package com.wrs.examsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wrs.examsystem.entity.Result.R;
import com.wrs.examsystem.entity.Student;
import com.wrs.examsystem.entity.Teacher;
import com.wrs.examsystem.entity.User;
import com.wrs.examsystem.entity.bean.EnrollBean;
import com.wrs.examsystem.entity.bean.LoginBean;
import com.wrs.examsystem.entity.bean.NewUserBean;
import com.wrs.examsystem.service.StudentService;
import com.wrs.examsystem.service.TeacherService;
import com.wrs.examsystem.service.UserService;
import com.wrs.examsystem.util.MD5Util;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王荣升
 * @since 2024-05-15
 */
@RestController
@RequestMapping("/examsystem/user")
public class UserController {
    @Resource
    UserService userService;
    @Resource
    StudentService studentService;
    @Resource
    TeacherService teacherService;


    @PostMapping("login")
    public R login(@RequestBody LoginBean loginBean){
        if (loginBean == null){
            return R.error().message("提交值为空");
        }else if (loginBean.getUserType() > 2 || loginBean.getUserType() < 0){
            return R.error().message("用户类型不合法");
        }
        return userService.login(loginBean);
    }
    @PostMapping("enroll")
    public R enroll(@RequestBody EnrollBean enrollBean){
        if (enrollBean.getUserType() == 0){
            return R.error().message("超级管理员只能有一个");
        }else if (enrollBean.getUserType() < 0 || enrollBean.getUserType() > 2){
            return R.error().message("用户类型不合法");
        }
//        检擦用户名是否存在
        QueryWrapper<User> wrapperByUsername = new QueryWrapper<>();
        wrapperByUsername.eq("username",enrollBean.getNickname());
        User user = userService.getOne(wrapperByUsername);
        if (user != null){
            return R.error().message("用户名已存在");
        }
//        检查编号是否存在
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("student_number",enrollBean.getNumber());
        Student student = studentService.getOne(studentQueryWrapper);
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("teacher_number",enrollBean.getNumber());
        Teacher teacher = teacherService.getOne(teacherQueryWrapper);
        if (teacher != null || student != null){
            return R.error().message("编号已存在");
        }
//        检查班级是否合法
        String[] classes = enrollBean.getClasses().split("\\|");
        if (enrollBean.getUserType() == 1 && classes.length > 1){
            return R.error().message("一个学生只能有一个班级");
        }
        if (enrollBean.getUserType() == 2 && classes.length < 1){
            return R.error().message("教师至少教授一个班级");
        }
        return userService.enroll(enrollBean);
    }

    /**
     * TODO 通过核验之后才能更改密码
     * 更改密码接口
     * @param newUserBean 用户名和新旧密码
     * @return
     */

    @PostMapping("changePassword")
    public R changePassword(@RequestBody NewUserBean newUserBean){
       return userService.checkUser(newUserBean);
    }

    /**
     * 通过管理员重置密码  学号+flyeat
     * @param username
     * @return
     */
    @PostMapping("resetPassword")
    public R resetPassword(@RequestParam String username){
        return userService.updateByName(username);
    }

    /**
     * 更改个人信息,不能更改密码
     * @param enrollBean 注册类
     * @return
     */
    @PostMapping("changeInformation")
    public R changeInformation(@RequestBody EnrollBean enrollBean){
        return userService.updateInformation(enrollBean);
    }
}

